﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.PM.Shell.Dialogs
{
    /// <summary>
    /// Interaction logic for SyncDlg.xaml
    /// </summary>
    public partial class SyncDlg : Window
    {
        public SyncDlg()
        {
            InitializeComponent();

            for (int index = 0; index < UIContext.Instance.Project.WorkItemCategoryList.Count; index++)
            {
                WorkItemCategory item = UIContext.Instance.Project.WorkItemCategoryList[index];
                if (item.State != BD_State.None)
                {
                    TreeViewItem node = new TreeViewItem();
                    node.Header = item.Caption;
                    _ctlTreeView.Items.Add(node);
                }
            }

            for (int index = 0; index < UIContext.Instance.Project.WorkItemList.Count; index++)
            {
                WorkItem item = UIContext.Instance.Project.WorkItemList[index];
                if (item.State != BD_State.None)
                {
                    TreeViewItem node = new TreeViewItem();
                    node.Header = item.Caption;
                    _ctlTreeView.Items.Add(node);
                }
            }

            for (int index = 0; index < UIContext.Instance.Project.WorkSheetList.Count; index++)
            {
                WorkSheet workSheet = UIContext.Instance.Project.WorkSheetList[index];

                TreeViewItem wsNode = new TreeViewItem();
                wsNode.Header = workSheet.Caption;
                wsNode.IsExpanded = true;
                _ctlTreeView.Items.Add(wsNode);

                //for (int iWIRef = 0; iWIRef < workSheet.WorkItemCollection.Count; iWIRef++)
                //{
                //    WorkItemRef item = workSheet.WorkItemCollection[iWIRef];
                //    if (item.State != BD_State.None)
                //    {
                //        TreeViewItem node = new TreeViewItem();
                //        node.Header = item.Caption;
                //        wsNode.Items.Add(node);
                //    }
                //}
            }
        }

        private void _ctlBtnCreate_Click_1(object sender, RoutedEventArgs e)
        {
            // 1.同步基础数据
            // 1.1.同步工作项类别
            for (int index = 0; index < UIContext.Instance.Project.WorkItemCategoryList.Count; index++)
            {
                WorkItemCategory item = UIContext.Instance.Project.WorkItemCategoryList[index];
                if (item.State == BD_State.Added)
                {
                    // 添加
                    PMServiceRef.WorkItemCategory newWorkItemCategory = new PMServiceRef.WorkItemCategory();
                    newWorkItemCategory.Id = item.Id;
                    newWorkItemCategory.Caption = item.Caption;
                    new PMServiceRef.PMWebServiceSoapClient().CreateWorkItemCategory(UIContext.Instance.Account, UIContext.Instance.Project.Id, newWorkItemCategory);
                    item.State = BD_State.None;
                }
            }

            // 1.2.同步工作项
            for (int index = 0; index < UIContext.Instance.Project.WorkItemList.Count; index++)
            {
                WorkItem item = UIContext.Instance.Project.WorkItemList[index];
                if (item.State == BD_State.Added)
                {
                    // 创建
                    PMServiceRef.WorkItem newWorkItem = new PMServiceRef.WorkItem();
                    newWorkItem.Id = item.Id;
                    newWorkItem.Caption = item.Caption;
                    new PMServiceRef.PMWebServiceSoapClient().CreateWorkItem(UIContext.Instance.Account, UIContext.Instance.Project.Id, item.WorkItemCategoryId, newWorkItem);
                    
                    item.State = BD_State.None;
                }
            }

            // 同步工作表结构
            for (int index = 0; index < UIContext.Instance.Project.WorkSheetList.Count; index++)
            {
                WorkSheet workSheet = UIContext.Instance.Project.WorkSheetList[index];

                //for (int iWIRef = 0; iWIRef < workSheet.WorkItemCollection.Count; iWIRef++)
                //{
                //    WorkItemRef item = workSheet.WorkItemCollection[iWIRef];
                //    if (item.State != BD_State.None)
                //    {
                //        PMServiceRef.WorkItemRef newWorkItemRef = new PMServiceRef.WorkItemRef();
                //        newWorkItemRef.Id = item.Id;
                //        newWorkItemRef.WorkItemCategoryId = item.WorkItemCategoryId;
                //        new PMServiceRef.PMWebServiceSoapClient().CreateWorkItemRef(UIContext.Instance.Account,
                //            UIContext.Instance.Project.Id,
                //            workSheet.Id,
                //            newWorkItemRef);
                //        item.State = BD_State.None;
                //    }
                //}
            }

            // 同步工作表数据
            for (int index = 0; index < UIContext.Instance.Project.WorkSheetList.Count; index++)
            {
                WorkSheet workSheet = UIContext.Instance.Project.WorkSheetList[index];

                List<PMServiceRef.WorkSheetCell> updatedCellList = new List<PMServiceRef.WorkSheetCell>();
                for (int iRow = 0; iRow < workSheet.RowCollection.Count; iRow++)
                {
                    WorkSheetRow row = workSheet.RowCollection[iRow];
                    for (int iCell = 0; iCell < row.CellCollection.Count; iCell++)
                    {
                        WorkSheetCell cell = row.CellCollection[iCell];
                        if (cell.State != BD_State.None)
                        {
                            PMServiceRef.WorkSheetCell updatedCell = new PMServiceRef.WorkSheetCell();
                            updatedCell.Path = cell.Path;
                            updatedCell.Value = cell.Value.ToString();
                            updatedCell.Text = string.Empty;
                            updatedCellList.Add(updatedCell);

                            cell.State = BD_State.None;
                        }
                    }
                }
                new PMServiceRef.PMWebServiceSoapClient().UpdateWorkSheetCells(UIContext.Instance.Account,
                    UIContext.Instance.Project.Id,
                    workSheet.Id,
                    updatedCellList.ToArray());
            }

            new PMService().SaveProject(UIContext.Instance.Project);

            MessageBox.Show("同步完成", "提示", MessageBoxButton.OK, MessageBoxImage.Information);

            DialogResult = true;
        }
    }
}
